
package com.ibeeking.found.report.rest.controller.v1;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.entity.CurrentUser;
import com.ibeeking.found.report.rest.service.IBaseService;
import com.ibeeking.found.report.rest.service.IDataSetService;
import com.ibeeking.found.report.rest.service.util.ReportUtils;
import com.ibeeking.found.report.service.common.dos.DataSetDO;
import com.ibeeking.found.report.service.common.param.DataSetTestTransformParam;
import com.ibeeking.found.report.service.common.po.ResponseBean;
import com.ibeeking.found.report.service.common.query.DataSetPageQuery;
import com.ibeeking.found.report.service.common.vo.DataSetVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* @desc 数据集 controller
* @website https://gitee.com/anji-plus/gaea
* @author Raod
* @date 2021-03-18 12:11:31.150755900
**/
@RestController
@RequestMapping("/dataSet/v1")
public class DmpDataSetController extends DmpBaseController<DataSetPageQuery, DataSetDO, DataSetVO> {

    @Resource
    private IDataSetService dataSetServiceImpl;

    @Override
    public IBaseService<DataSetPageQuery, DataSetDO> getService() {
        return dataSetServiceImpl;
    }

    @Override
    public DataSetDO getEntity() {
        return new DataSetDO();
    }

    @Override
    public DataSetVO getDTO() {
        return new DataSetVO();
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询数据集")
    @GetMapping("/detailBysetId/{id}")
    public ResponseBean detailBysetId(@PathVariable("id") Long id) {
        this.logger.info("{}根据ID查询服务开始，id为：{}", this.getClass().getSimpleName(), id);
        ResponseBean responseBean = this.responseSuccessWithData(dataSetServiceImpl.detailSet(id));
        this.logger.info("{}根据ID查询结束，结果：{}", this.getClass().getSimpleName(), ReportUtils.toJSONString(responseBean));
        return responseBean;
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询数据集")
    @GetMapping({"/detailBysetCode/{setCode}"})
    public ResponseBean detailBysetCode(@PathVariable("setCode") String setCode) {
        this.logger.info("{}根据setCode查询服务开始，setCode为：{}", this.getClass().getSimpleName(), setCode);
        DataSetVO datasetVO = dataSetServiceImpl.detailSet(setCode);
        ResponseBean responseBean = this.responseSuccessWithData(datasetVO);
        this.logger.info("{}根据setCode查询结束，结果：{}", this.getClass().getSimpleName(), ReportUtils.toJSONString(responseBean));
        return responseBean;
    }

    @Override
    public ResponseBean insert(@LoginUser CurrentUser loginUser, @RequestBody DataSetVO dto) {
        this.logger.info("{}新增服务开始，参数：{}", this.getClass().getSimpleName(), ReportUtils.toJSONString(dto));
        DataSetVO dataSetVO = dataSetServiceImpl.insertSet(dto);
        this.logger.info("{}新增服务结束，结果：{}", this.getClass().getSimpleName(), ReportUtils.toJSONString(dataSetVO));
        return ResponseBean.builder().data(dataSetVO).build();
    }

    @PutMapping
    @Override
    public ResponseBean update(@LoginUser CurrentUser loginUser, @RequestBody DataSetVO dto) {
        String username = loginUser.getName();
        this.logger.info("{}更新服务开始,更新人：{}，参数：{}", this.getClass().getSimpleName(), username, ReportUtils.toJSONString(dto));
        ResponseBean responseBean = this.responseSuccess();
        dataSetServiceImpl.updateSet(dto);
        this.logger.info("{}更新服务结束，结果：{}", this.getClass().getSimpleName(), ReportUtils.toJSONString(responseBean));
        return this.responseSuccess();
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除数据集")
    @DeleteMapping({"/{id}"})
    @Override
    public ResponseBean deleteById(@PathVariable("id") Long id) {
        this.logger.info("{}删除服务开始，参数ID：{}", this.getClass().getSimpleName(), id);
        dataSetServiceImpl.deleteSet(id);
        this.logger.info("{}删除服务结束", this.getClass().getSimpleName());
        return this.responseSuccess();
    }

    /**
     * 测试 数据转换是否正确
     * @param param
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "数据转换是否正确")
    @PostMapping("/testTransform")
    public ResponseBean testTransform(@Validated @RequestBody DataSetTestTransformParam param) {
        DataSetVO dto = new DataSetVO();
        BeanUtils.copyProperties(param, dto);
        return responseSuccessWithData(dataSetServiceImpl.testTransform(dto));
    }

    /**
     * 获取所有数据集
     * @return
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取所有数据集")
    @GetMapping("/queryAllDataSet")
    public ResponseBean queryAllDataSet() {
        List<DataSetDO> dataSetDOList = dataSetServiceImpl.queryAllDataSet();
        return responseSuccessWithData(dataSetDOList);
    }


}
